<?php
// No directAcces
defined('_AppBackMiage') or die('Restricted access');
if(isAdministrateur ()){
    include_once (DS . "backoffice/include/header.php");
    include_once( DS . "backoffice/include/toppanel_utilisateurs.php");

    $lstPromo = Promo::getListPromo();
    ?>
    <div id="wrapper">
        <div id="content">
            <div id="rightnow">
                <?php
                $title = "Etudiants";
                $add = '<a href="' . URL . 'index.php?m=admin_useradd_etudiant" class="add">Ajouter un &eacute;tudiant</a>';
                $csv = '<a href="' . URL . 'index.php?m=admin_userimport_etudiant" class="app_add">Ajouter Massif (csv)</a>';
                ?>

                <h3 class="reallynow">
                    <span>Listes des <?php echo $title; ?></span>
                    <?php echo $add;
                    echo $csv; ?>
                    <br />
                </h3>
                <p class="youhave">
                <form action="<?php echo URL; ?>index.php" method="get">
                    <input type="hidden" name="m" value="admin_useretudiant_list"/>
                    <label for="name">Recherche : </label><input type="text" name="name" id="name" value="<?php echo $_GET['name']; ?>"/>
                    <label for="promo">Promo : </label>
                    <select name="promo">
                        <option value="0">-- Tous --</option>
                        <?php
                        foreach ($lstPromo as $i => $p)
                        {

                            echo "<option value='" . $i . "'";
                            if (isset($_GET['promo']) && $_GET["promo"] == $i)
                                echo " selected='selected' ";
                            echo ">" . $p . "</option>";
                        }
                        ?>
                    </select>
                    <input type="submit" value="Rechercher"/>
                </form>
                </p>
                <p class="youhave">
                    <?php
                        $nbrResultPerPage = 10;

                        if (isset($_GET['page']) && !empty($_GET['page']))
                        {
                            $page = $_GET['page'];
                            $nbr = (((int) $_GET['page'] - 1) * $nbrResultPerPage);
                            $lstUtilisateur = Etudiant::getList($nbrResultPerPage, $nbr, $_GET['name'], $_GET['promo']);
                        } else
                        {
                            $page = 1;
                            $lstUtilisateur = Etudiant::getList($nbrResultPerPage, 0, $_GET['name'], $_GET['promo']);
                        }

                        $count = Etudiant::globalCount($_GET['name'], $_GET['promo']);
                        $pages = ceil($count / $nbrResultPerPage);
                        $pageSuivante = $page + 1;
                        $pagePrecendente = $page - 1;
                        if (!empty($lstUtilisateur))
                        {
                    ?>
                        <table width="100%">
                            <thead>
                                <tr>
                                    <th>Nom</th>
                                    <th>Email</th>
                                    <th>Date de derni&egrave;re visite</th>
                                    <th>Type de compte</th>
                                    <th>Promo</th>
                                    <th width="60px">Action</th>
                                </tr>
                            </thead>
                            <tbody>
                        <?php foreach ($lstUtilisateur as $u)
                            {
     ?>
                        <?php
                                $viewURL = URL . "/index.php?m=admin_userview_etudiant&uid=" . $u->IdUtilisateur;
                                $editURL = URL . "/index.php?m=admin_useredit_etudiant&uid=" . $u->IdUtilisateur;
                        ?>
                                <tr>
                                    <td><?php echo $u->NomUtilisateur . ' ' . $u->PrenomUtilisateur; ?></td>
                                    <td><?php echo $u->EmailUtilisateur; ?></td>
                                    <td><?php echo date(DATE_FORMAT, $u->DateDerniereVisite); ?></td>
                                    <td><?php echo Utilisateur::getTypeById($u->IdUtilisateur); ?></td>
                                    <td><?php echo Promo::getPromoById($u->idPromo); ?></td>
                                    <td>
                                        <a href="<?php echo $viewURL; ?>">
                                            <img src="<?php echo STYLEBK; ?>img/icons/user.png" title="Show profile" width="16" height="16" />
                                        </a>
                                        <a href="<?php echo $editURL; ?>">
                                            <img src="<?php echo STYLEBK; ?>img/icons/user_edit.png" title="Edit user" width="16" height="16" />
                                        </a>
                                    </td>
                                </tr>
    <?php } ?>
                        </tbody>
                    </table>
                    <div id="pager">
                        Page
                    <?php if ($pagePrecendente > 0): ?>
                                <a href="<?php echo URL . "/index.php?m=admin_useretudiant_list&page=" . $pagePrecendente; ?>&tid=0&name=<?php echo $_GET['name']; ?>&promo=<?php echo $_GET['promo']; ?>"><img src="<?php echo STYLEBK; ?>img/icons/arrow_left.gif" width="16" height="16" /></a>
                    <?php endif; ?>
    <?php echo $page; ?>
                    <?php if ($pageSuivante <= $pages): ?>
                                    <a href="<?php echo URL . "/index.php?m=admin_useretudiant_list&page=" . $pageSuivante; ?>&tid=0&name=<?php echo $_GET['name']; ?>&promo=<?php echo $_GET['promo']; ?>"><img src="<?php echo STYLEBK; ?>img/icons/arrow_right.gif" width="16" height="16" /></a>
    <?php endif; ?>
                                    sur <?php echo $pages; ?> | Total <strong><?php echo $count; ?></strong> &eacute;tudiants trouv&eacute;s
                                    </div>
                <?php
                                }
                                else
                                {
                                    echo 'rien';
                                }
                ?>
                                </p>
                            </div>
                        </div>
        <?php
        include_once (DS . "backoffice/include/sidebar.php");
        include_once (DS . "backoffice/include/footer.php");
}
else
{
    redirectTo(URLBACK);
}
?>